<?php

/**
 * crpCalendar
 *
 * @copyright (c) 2007,2009 Daniele Conca
 * @link http://code.zikula.org/crpcalendar Support and documentation
 * @author Daniele Conca <conca.daniele@gmail.com>
 * @license GNU/GPL - v.2.1
 * @package crpCalendar
 */

/**
 * initialise block
 *
 */
function crpCalendar_eventsblock_init()
{
	// Security
	pnSecAddSchema('Eventsblock::', 'Block title::');
}

/**
 * get information on block
 *
 */
function crpCalendar_eventsblock_info()
{
	return array (
		'text_type' => 'crpEvents',
		'module' => 'crpCalendar',
		'text_type_long' => 'Events Titles',
		'allow_multiple' => true,
		'form_content' => false,
		'form_refresh' => false,
		'show_preview' => true
	);
}

/**
 * display block
 *
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the rendered bock
 */
function crpCalendar_eventsblock_display($blockinfo)
{
	// security check
	if (!SecurityUtil :: checkPermission('Eventsblock::', "$blockinfo[title]::", ACCESS_READ))
		return;

	if (!pnModAvailable('crpCalendar'))
		return;

	// get the current language
	$currentlang = pnUserGetLang();

	// Break out options from our content field
	$vars = pnBlockVarsFromContent($blockinfo['content']);

	if (!isset ($vars['limit']))
		$vars['limit'] = 10;

	if (!isset ($vars['numitems']))
		$vars['numitems'] = 10;

	$apiargs['interval'] = $vars['limit'];
	$apiargs['startnum'] = 1;
	$apiargs['active'] = 'A';
	$apiargs['modvars']['itemsperpage'] = $vars['numitems'];
	$apiargs['sortOrder'] = 'ASC';

	// call the api
	$items = pnModAPIFunc('crpCalendar', 'user', 'getall', $apiargs);

	// create the output object
	$pnRender = pnRender :: getInstance('crpCalendar', false);

	$pnRender->assign('events', $items);
	$pnRender->assign('interval', $vars['limit']);
	$pnRender->assign(pnModGetVar('crpCalendar'));

	$blockinfo['content'] = $pnRender->fetch('blocks/crpcalendar_block_events.htm');
	return pnBlockThemeBlock($blockinfo);
}

/**
 * modify block settings
 *
 * @param        array       $blockinfo     a blockinfo structure
 * @return       output      the bock form
 */
function crpCalendar_eventsblock_modify($blockinfo)
{
	// Break out options from our content field
	$vars = pnBlockVarsFromContent($blockinfo['content']);

	// Defaults
	if (empty ($vars['limit']))
		$vars['limit'] = 10;

	if (!isset ($vars['numitems']))
		$vars['numitems'] = 10;

	// Create output object
	$pnRender = pnRender :: getInstance('crpCalendar', false);

	// assign the block vars
	$pnRender->assign($vars);

	// Return the output that has been generated by this function
	return $pnRender->fetch('blocks/crpcalendar_block_events_modify.htm');

}

/**
 * update block settings
 *
 * @param        array       $blockinfo     a blockinfo structure
 * @return       $blockinfo  the modified blockinfo structure
 */
function crpCalendar_eventsblock_update($blockinfo)
{
	// Get current content
	$vars = pnBlockVarsFromContent($blockinfo['content']);

	// alter the corresponding variable
	$vars['limit'] = (int) FormUtil :: getPassedValue('limit', null, 'POST');
	$vars['numitems'] = (int) FormUtil :: getPassedValue('numitems', null, 'POST');

	// write back the new contents
	$blockinfo['content'] = pnBlockVarsToContent($vars);

	// clear the block cache
	$pnRender = pnRender :: getInstance('crpCalendar');
	$pnRender->clear_cache('blocks/crpcalendar_block_events.htm');

	return $blockinfo;
}